/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.tools;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.mime.MultipartEntity;

import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author Administrator
 */
public class HttpClient {

    public static String httpPost(String url, List<NameValuePair> list) {
        String result = null;
        try {
            // (1) Post请求
            HttpPost post = new HttpPost(url);

//List<NameValuePair> params = new ArrayList<NameValuePair>();  
//params.add(new BasicNameValuePair("name", "ahopedog"));  
//params.add(new BasicNameValuePair("work", "程序员"));  
//            
            post.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
            System.out.println(url + " -------------- " + list.size());

            // (3) 发送请求
            org.apache.http.client.HttpClient http = new DefaultHttpClient();
            HttpResponse response = http.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, HTTP.UTF_8);

                System.out.println("-----------------postresult:" + result);
                return result;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String uploadFile(String urls, String filePath) throws IOException {
        try {
            URL url = new URL(urls);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty("FileName", "test");

            connection.setRequestProperty("content-type", "text/html");
            BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());

            //读取文件上传到服务器
            File file = new File(filePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            int numReadByte = 0;
            byte[] bytes = new byte[1024];
            while ((numReadByte = fileInputStream.read(bytes, 0, 1024)) > 0) {
                out.write(bytes, 0, numReadByte);
            }

            out.flush();
            fileInputStream.close();
            //读取URLConnection的响应
            InputStream inStream = connection.getInputStream();
            return new String(readInputStream(inStream), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();//网页的二进制数据
        outStream.close();
        inStream.close();
        return data;
    }

    /**
     * 上传文件
     *
     * @param filePath
     * @param url
     * @return
     */
    public static String postFile(String filePath, String url) {

        org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
        //设置通信协议版本
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpPost httpPost = new HttpPost(url);
        MultipartEntity mpEntity = new MultipartEntity(); //文件传输
        ContentBody cbFile = new FileBody(new File(filePath));

        mpEntity.addPart("file", cbFile); // <input type="file" name="file" />  对应的
        httpPost.setEntity(mpEntity);

        HttpResponse httpResponse = null;
        HttpEntity resEntity = null;
        String result = null;
        try {
            httpResponse = httpclient.execute(httpPost);
            resEntity = httpResponse.getEntity();
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(resEntity, "utf-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (resEntity != null) {
                try {
                    resEntity.consumeContent();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            httpclient.getConnectionManager().shutdown();
        }

        return result;
    }

    public static void main(String[] args) {

        try {
            String url = "http://localhost:8080/MedicalInfo/patient/lisDetail";
            String list_info = "<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"no\"?><dw_interface_send_info><dw_interface_send_info_row><order_id>258</order_id><date_test>2016-03-28 00:00:00</date_test><sample_no>LA0013</sample_no><patient_name>刀红良</patient_name><patient_sex>1</patient_sex><patient_age>43</patient_age><reporter>lzy</reporter><checker>lzy</checker><diagnosis></diagnosis><ward_code></ward_code><office>1</office><bed_no></bed_no><doctor>8</doctor><date_print>2016-04-03 10:41:21.300000</date_print><require_date>2016-03-28 17:10:58.850000</require_date><date_check>2016-03-28 17:10:58.850000</date_check><date_report>2016-04-03 10:41:21.300000</date_report><remark_info></remark_info><charge_type></charge_type><module_kind>1</module_kind><patient_kind>1</patient_kind><report_type>0</report_type><patient_code></patient_code><patient_birthday></patient_birthday></dw_interface_send_info_row></dw_interface_send_info>";
            String result = "<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"no\"?>\n"
                    + "\n"
                    + "<dw_interface_send_detail><dw_interface_send_detail_row><item_code>HBcAb</item_code><item_name>乙肝病毒核心抗体IgG</item_name><test_value>阴性(-)</test_value><flag_yq>手工</flag_yq><text_range></text_range><text_danwei></text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>HBeAb</item_code><item_name>乙肝病毒e抗体</item_name><test_value>阴性(-)</test_value><flag_yq>手工</flag_yq><text_range></text_range><text_danwei></text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>HBeAg</item_code><item_name>乙肝病毒e抗原</item_name><test_value>阴性(-)</test_value><flag_yq>手工</flag_yq><text_range></text_range><text_danwei></text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>HBsAb</item_code><item_name>乙肝病毒表面抗体</item_name><test_value>阴性(-)</test_value><flag_yq>手工</flag_yq><text_range></text_range><text_danwei></text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>HBsAg</item_code><item_name>乙肝表面抗原</item_name><test_value>阴性(-)</test_value><flag_yq>手工</flag_yq><text_range></text_range><text_danwei></text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>HCT</item_code><item_name>红细胞压积</item_name><test_value>0.420</test_value><flag_yq>KX-21N</flag_yq><text_range>0.350-0.500</text_range><text_danwei>L/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>HGB</item_code><item_name>血红蛋白</item_name><test_value>147</test_value><flag_yq>KX-21N</flag_yq><text_range>110-150</text_range><text_danwei>g/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>LYM#</item_code><item_name>淋巴细胞绝对值</item_name><test_value>0.5</test_value><flag_yq>KX-21N</flag_yq><text_range>1.0-3.3</text_range><text_danwei>10^9/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>LYM%</item_code><item_name>淋巴细胞百分比</item_name><test_value>4</test_value><flag_yq>KX-21N</flag_yq><text_range>20-50</text_range><text_danwei>%</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>MCH</item_code><item_name>平均血红蛋白量</item_name><test_value>33.2</test_value><flag_yq>KX-21N</flag_yq><text_range>28.0-33.0</text_range><text_danwei>pg</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>MCHC</item_code><item_name>平均血红蛋白浓度</item_name><test_value>348</test_value><flag_yq>KX-21N</flag_yq><text_range>320-360</text_range><text_danwei>g/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>MCV</item_code><item_name>红细胞平均体积</item_name><test_value>95.5</test_value><flag_yq>KX-21N</flag_yq><text_range>80-100</text_range><text_danwei>fL</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>MPV</item_code><item_name>平均血小板体积</item_name><test_value>8.4</test_value><flag_yq>KX-21N</flag_yq><text_range>7.0-12.0</text_range><text_danwei>fL</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>MXD#</item_code><item_name>单核细胞绝对值</item_name><test_value>0.10</test_value><flag_yq>KX-21N</flag_yq><text_range>0.3-0.8</text_range><text_danwei>10^9/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>MXD%</item_code><item_name>单核细胞百分率</item_name><test_value>1.4</test_value><flag_yq>KX-21N</flag_yq><text_range>4-10</text_range><text_danwei>%</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>NEUT#</item_code><item_name>中性粒细胞绝对值</item_name><test_value>10.00</test_value><flag_yq>KX-21N</flag_yq><text_range>1.8-6.3</text_range><text_danwei>10^9/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>NEUT%</item_code><item_name>中性粒细胞百分比</item_name><test_value>94.30</test_value><flag_yq>KX-21N</flag_yq><text_range>40-75</text_range><text_danwei>%</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>PDW</item_code><item_name>血小板分布宽度</item_name><test_value>9.3</test_value><flag_yq>KX-21N</flag_yq><text_range>15.5-18.2</text_range><text_danwei>fL</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>P-LCR</item_code><item_name>大血小板比率</item_name><test_value>13.7</test_value><flag_yq>KX-21N</flag_yq><text_range>13-43</text_range><text_danwei>%</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>PLT</item_code><item_name>血小板</item_name><test_value>203</test_value><flag_yq>KX-21N</flag_yq><text_range>125-350</text_range><text_danwei>10^9/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>RBC</item_code><item_name>红细胞</item_name><test_value>4.40</test_value><flag_yq>KX-21N</flag_yq><text_range>3.8-5.1</text_range><text_danwei>10^12/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>RDW-CV</item_code><item_name>红细胞平均宽度</item_name><test_value>13.10</test_value><flag_yq>KX-21N</flag_yq><text_range>11.5-14.4</text_range><text_danwei>%</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>RDW-SD</item_code><item_name>红细胞分布宽度</item_name><test_value>43.8</test_value><flag_yq>KX-21N</flag_yq><text_range>37.0-55.0</text_range><text_danwei>fL</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row><dw_interface_send_detail_row><item_code>WBC</item_code><item_name>白细胞</item_name><test_value>10.60</test_value><flag_yq>KX-21N</flag_yq><text_range>3.5-9.5</text_range><text_danwei>10^9/L</text_danwei><text_note></text_note><sample_no>LA0013</sample_no><date_test>2016-03-28 00:00:00</date_test></dw_interface_send_detail_row></dw_interface_send_detail>";
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("patient_info", list_info));
            params.add(new BasicNameValuePair("result", result));
            String hostId = HttpClient.httpPost(url, params);
            System.out.println(hostId + "********************************");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
